package service

import (
	"context"
	"encoding/json"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
	"zhuandan/redis_lib"
)

func Citys() []models.City {
	var citys []models.City
	ctx := context.Background()
	cityslist, _ := redis_lib.Client.Get(ctx, "jiazhengcitys").Result()
	if cityslist == "" || len(cityslist) == 0 || cityslist == "null" {
		data_source.Db.Find(&citys)
		byte_data, _ := json.Marshal(citys)
		str := string(byte_data)
		redis_lib.Client.Set(ctx, "jiazhengcitys", str, -1)
	} else {
		json.Unmarshal([]byte(cityslist), &citys)
	}
	return citys
}
func Kefus() []models.Adminuser {
	var citys []models.Adminuser
	ctx := context.Background()
	cityslist, _ := redis_lib.Client.Get(ctx, "adminuserlist").Result()
	if cityslist == "" || len(cityslist) == 0 || cityslist == "null" {
		data_source.Db.Find(&citys)
		byte_data, _ := json.Marshal(citys)
		str := string(byte_data)
		redis_lib.Client.Set(ctx, "adminuserlist", str, 86400*time.Second)
	} else {
		json.Unmarshal([]byte(cityslist), &citys)
	}
	return citys
}
